package com.dlxj.dao;

import java.io.Serializable;
import java.sql.SQLException;
import java.util.List;

import org.hibernate.HibernateException;
import org.hibernate.Query;
import org.hibernate.SQLQuery;
import org.hibernate.Session;
import org.springframework.orm.hibernate3.HibernateCallback;
import org.springframework.orm.hibernate3.support.HibernateDaoSupport;

import com.dlxj.util.PageBean;

public class BaseDAO extends HibernateDaoSupport {

	public List findByHql(String hql) {

		return getHibernateTemplate().find(hql);

	}

	public List findByHql(String hql, Object[] objs) {

		return getHibernateTemplate().find(hql, objs);

	}

	public Object findById(Class clazz, Serializable id) {

		return getSession().get(clazz, id);
	}

	public Object save(Object obj) {

		return getHibernateTemplate().save(obj);
	}

	public void saveOrUpdate(Object obj) {

		getHibernateTemplate().saveOrUpdate(obj);
	}

	public List findBySql(String sql) {

		return getSession().createSQLQuery(sql).list();
	}

	public void delete(Class clazz, Serializable id) {
		getHibernateTemplate().delete(this.findById(clazz, id));
	}

	@SuppressWarnings("unchecked")
	public void listDelete(final String id) {
		getHibernateTemplate().execute(new HibernateCallback() {

			@Override
			public Object doInHibernate(Session session)
					throws HibernateException, SQLException {
				session.createQuery(
						"delete from CircuitryPole c where c.cid = '" + id
								+ "'").executeUpdate();
				return null;
			}

		});
	}

	public void update(Object obj) {
		getHibernateTemplate().update(obj);

	}

	public PageBean findPageBeanHQL(String hql, int pageIndex, int pageSize,
			Object[] params) {

		PageBean pageBean = new PageBean();

		// 拼接处查询count 的hql语句
		String countHql = hql.substring(hql.indexOf("from "));

		int pos = countHql.toLowerCase().indexOf("order by");

		if (pos != -1) {

			countHql = countHql.substring(0, pos);

		}

		countHql = "select count(*) " + countHql;

		Session session = getSession();

		Query q = session.createQuery(countHql);

		// 设置count的条件
		setparams(params, q);
		int count = Integer.parseInt(q.uniqueResult().toString());
		pageBean.setTotal(count);

		// 开始查询当前页数据
		Query q2 = session.createQuery(hql);

		// 设置条件
		setparams(params, q2);

		q2.setFirstResult((pageIndex - 1) * pageSize);

		q2.setMaxResults(pageSize);

		pageBean.setRows(q2.list());

		int pageCount = count / pageSize + (count % pageSize == 0 ? 0 : 1);

		pageBean.setPageCount(pageCount);

		pageBean.setIndex(pageIndex);

		pageBean.setPageSize(pageSize);

		return pageBean;

	}

	private void setparams(Object[] params, Query q) {

		if (params != null && params.length > 0) {

			for (int i = 0; i < params.length; i++) {

				q.setParameter(i, params[i]);

			}

		}
	}

	public int countHql(String hql, Object[] params) {

		Session session = getSession();

		Query q = session.createQuery(hql);
		setparams(params, q);

		return Integer.parseInt(q.uniqueResult().toString());

	}

	public int countSql(String sql, Object[] params) {

		Session session = getSession();

		SQLQuery q = session.createSQLQuery(sql);

		setparams(params, q);

		return Integer.parseInt(q.uniqueResult().toString());

	}

}
